package org.example.day;

import org.example.linkedlist.ListNode;

/**
 * @Description: TODO
 * @Author wyatt
 * @Data 2024/09/09 22:14
 */
public class Solution2181 {

    public static void main(String[] args) {
        ListNode head = new ListNode(0, new ListNode(3, new ListNode(1, new ListNode(0, new ListNode(4, null)))));
        ListNode res = new Solution2181().mergeNodes(head);

        System.out.println(res.val);
        while (res.next != null){
            System.out.println(res.val);
            res = res.next;
        }
    }


    public ListNode mergeNodes(ListNode head) {

        ListNode res = new ListNode(0);
        ListNode resNext = res;

        ListNode headNext = head;

        while (head.next != null){
            if(headNext.val == 0){
                //作为一个新起点
                resNext.next = new ListNode(0);
                resNext = resNext.next;
            }else {
                resNext.val += headNext.val;
            }

            headNext = head.next;
            head = headNext;
        }

        return res.next;
    }

}
